﻿<%@ Page Language="C#" MasterPageFile="~/MasterPages/Core.Master" AutoEventWireup="true"
    CodeBehind="CreateBoard.aspx.cs" Inherits="Pap.Web.Modules.Loan.Views.CreateBoard"
    meta:resourcekey="PageResource1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <script type="text/javascript">
        function ValidateForm() {
            var store = Ext.getCmp('gridBoardMembers').getStore();
            var count = store.getCount();
            if (count < 1) {
                Ext.getCmp('dateHoldDate').markInvalid('<asp:Literal runat="server" Text="<%$ Resources:MemberCountError %>" />');
                Ext.getCmp('statusBar').setText('<asp:Literal runat="server" Text="<%$ Resources:MemberCountError %>" />');
                return false;
            }
            var loansSelected = Ext.getCmp('gridWaitingLoans').hasSelection();
            if (!loansSelected) {
                Ext.getCmp('dateHoldDate').markInvalid('<asp:Literal runat="server" Text="<%$ Resources:LoanCountError %>" />');
                Ext.getCmp('statusBar').setText('<asp:Literal runat="server" Text="<%$ Resources:LoanCountError %>" />');
                return false;
            }
            else {
                return Ext.getCmp('mainForm').getForm().isValid();
            }
        }

        var Check = function(value, rowIndex, record) {
            return !(record.data.Submitted);

        }


        var Filter = function(store) {
            store.filterBy(getRecordFilter());
        }

        var filterString = function(value, dataIndex, record) {
            var val = record.get(dataIndex);
            if (typeof val != "string") {
                return true;
            }
            if (value.length == 0) {
                return value.length == 0;
            }
            return val.toLowerCase().indexOf(value.toLowerCase()) > -1;
        }
        var getRecordFilter = function() {
            var f = [];

            f.push({
                filter: function(record) {
                    return filterString(Ext.getCmp('txtName').getValue(), 'Name', record);
                }
            });
            var len = f.length;
            return function(record) {
                for (var i = 0; i < len; i++) {
                    if (!f[i].filter(record)) {
                        return false;
                    }
                }
                return true
            };
        }
    </script>

    <ext:Store runat="server" ID="storeResults" AutoLoad="True" IDMode="Legacy" meta:resourcekey="storeResultsResource1"
        SerializationMode="Simple">
        <Reader>
            <ext:JsonReader ReaderID="Id">
                <Fields>
                    <ext:RecordField Name="Id" Mapping="BoardMemberID" />
                    <ext:RecordField Name="Name" Mapping="BoardMemberName" />
                </Fields>
            </ext:JsonReader>
        </Reader>
    </ext:Store>
    <ext:Store runat="server" ID="storeProjects" AutoLoad="True" IDMode="Legacy" meta:resourcekey="storeProjectsResource1"
        SerializationMode="Simple">
        <Reader>
            <ext:JsonReader ReaderID="ProjectID">
                <Fields>
                    <ext:RecordField Name="ProjectID" />
                    <ext:RecordField Name="ProjectName" />
                </Fields>
            </ext:JsonReader>
        </Reader>
    </ext:Store>
    <ext:Store runat="server" ID="storeMembers" AutoLoad="True" IDMode="Legacy" meta:resourcekey="storeMembersResource1"
        SerializationMode="Simple">
        <Reader>
            <ext:JsonReader ReaderID="ID">
                <Fields>
                    <ext:RecordField Name="ID" Mapping="BoardMemberID" />
                    <ext:RecordField Name="Name" Mapping="BoardMemberName" />
                </Fields>
            </ext:JsonReader>
        </Reader>
    </ext:Store>
    <ext:Store runat="server" ID="storeLoans" AutoLoad="True" IDMode="Legacy" meta:resourcekey="storeLoansResource1"
        SerializationMode="Simple">
        <Reader>
            <ext:JsonReader ReaderID="LoanID">
                <Fields>
                    <ext:RecordField Name="LoanID" />
                    <ext:RecordField Name="LoanNumber" />
                    <ext:RecordField Name="LoanCreatedDate" Type="Date" />
                    <ext:RecordField Name="CustomerName" />
                    <ext:RecordField Name="LoanType" />
                    <ext:RecordField Name="RequestedAmount" />
                    <ext:RecordField Name="ResponsibleProposedAmount" />
                    <ext:RecordField Name="RequestedPaymentPeriod" />
                    <ext:RecordField Name="PreviousNotes" />
                    <ext:RecordField Name="Submitted" />
                    <ext:RecordField Name="Notes" />
                </Fields>
            </ext:JsonReader>
        </Reader>
    </ext:Store>
    
    <ext:FitLayout runat="server" IDMode="Legacy" meta:resourcekey="fitLayout1Resource1">
        <ext:FormPanel ID="mainForm" IDMode="Static" runat="server" BodyStyle="padding:5px;"
            ButtonAlign="Right" AutoScroll="true" meta:resourcekey="mainFormResource1" Frame="true">
            <TopBar>
                <ext:Toolbar runat="server">
                    <Items>
                        <ext:ComboBox ValueField="ProjectID" FieldLabel="Project" DisplayField="ProjectName"
                            runat="server" ID="cmbProjects" meta:resourcekey="cmbProjectsResource" StoreID="storeProjects"
                             AllowBlank="False" TriggerAction="All" Enabled="true">
                            <AjaxEvents>
                                <Select OnEvent="CmbProjects_Selected">
                                    <EventMask ShowMask="True" />
                                </Select>
                            </AjaxEvents>
                        </ext:ComboBox>
                    </Items>
                </ext:Toolbar>
            </TopBar>
            <Body>
                <table width="100%">
                <tr>
                    <td>
                        <ext:DateField runat="server" ID="dateHoldDate" IDMode="Static" FieldLabel="Date to hold Board"
                             AutoWidth="true" AllowBlank="False" meta:resourcekey="dateHoldDateResource1" Format="dd-MM-yyyy">
                            <AjaxEvents>
                                <Select OnEvent="DateHoldDate_Selected">
                                    <EventMask ShowMask="true" />
                                </Select>
                            </AjaxEvents>
                        </ext:DateField>
                    </td>
                </tr>
                <tr>
                    <td>
                        <ext:GridPanel runat="server" ID="gridBoardMembers" IDMode="Static" Title="Board Members" 
                        AutoHeight="true"
                                    AutoExpandColumn="Name" StoreID="storeMembers" meta:resourcekey="gridBoardMembersResource1">
                                    <TopBar>
                                        <ext:Toolbar runat="server">
                                            <Items>
                                                <ext:Button ID="btnAddNewMember" runat="server" Icon="Add" meta:resourcekey="btnAddNewMember1">
                                                <AjaxEvents>
                                                    <Click OnEvent="gridBoardMembers_Click">
                                                    </Click>
                                                </AjaxEvents>
                                                </ext:Button>
                                            </Items>
                                        </ext:Toolbar>
                                    </TopBar>
                                    <ColumnModel>
                                        <Columns>
                                            <ext:Column ColumnID="Name" Header="<%$ Resources:HeaderName %>" DataIndex="Name"
                                                Sortable="True" />
                                            <ext:CommandColumn Header="<%$ Resources:HeaderRemoveFromBoard %>" Hideable="False"
                                                Sortable="True">
                                                <Commands>
                                                    <ext:GridCommand CommandName="RemoveMember" Text="<%$ Resources:TextRemove %>" Icon="Delete"
                                                        IconCls="icon-delete" />
                                                </Commands>
                                            </ext:CommandColumn>
                                        </Columns>
                                    </ColumnModel>
                                    <AjaxEvents>
                                        <Command OnEvent="gridMembers_Command">
                                            <ExtraParams>
                                                <ext:Parameter Name="CommandName" Value="command" Mode="Raw" Encode="False" />
                                                <ext:Parameter Name="UserId" Value="record.data.ID" Mode="Raw" Encode="False" />
                                                <ext:Parameter Name="DisplayName" Value="record.data.Name" Mode="Raw" Encode="False" />
                                            </ExtraParams>
                                            <EventMask MinDelay="200" Msg="<%$ Resources:TextUpdating %>" Target="CustomTarget"
                                                CustomTarget="#{gridBoardMembers}" ShowMask="True" />
                                        </Command>
                                    </AjaxEvents>
                         </ext:GridPanel>
                    </td>
                </tr>
                <tr>
                    <td>
                        <ext:GridPanel runat="server" ID="gridWaitingLoans" IDMode="Static" Title="Loans to discuss"
                            AutoExpandColumn="Name" StoreID="storeLoans" AutoHeight="true" AutoScroll="true"
                            meta:resourcekey="gridWaitingLoansResource1" SelectionSavingBuffer="10">
                            <ColumnModel>
                                <Columns>
                                    <ext:Column Header="<%$ Resources:HeaderLoanNumber %>" DataIndex="LoanNumber" Sortable="false" />
                                    <ext:Column Header="<%$ Resources:HeaderLoanDate %>" DataIndex="LoanCreatedDate"
                                        Sortable="false">
                                        <Renderer Fn="Ext.util.Format.dateRenderer('d/m/Y')" />
                                    </ext:Column>
                                    <ext:Column ColumnID="Name" Header="<%$ Resources:HeaderName %>" DataIndex="CustomerName"
                                        Sortable="false" />
                                    <ext:Column Header="<%$ Resources:HeaderLoanType %>" DataIndex="LoanType" Sortable="True" />
                                    <ext:Column Header="<%$ Resources:HeaderRequestedAmount %>" DataIndex="RequestedAmount"
                                        Sortable="false" />
                                    <ext:Column Header="<%$ Resources:HeaderResponsibleProposedAmount %>" DataIndex="ResponsibleProposedAmount"
                                        Sortable="false" />
                                    <ext:Column Header="<%$ Resources:HeaderPaymentPeriod %>" DataIndex="RequestedPaymentPeriod"
                                        Sortable="false" />
                                    <ext:Column DataIndex="PreviousNotes" Header="<%$ Resources:HeaderPreviousNotes %>" />
                                    <ext:Column Header="<%$ Resources:HeaderNotes %>" DataIndex="Notes" Width="250px"
                                        Sortable="false">
                                        <Editor>
                                            <ext:TextArea runat="server" DataIndex="Notes" MaxLength="250" />
                                        </Editor>
                                    </ext:Column>
                                </Columns>
                            </ColumnModel>
                            <View>
                                <ext:GridView ID="gridWaitingLoansView" ForceFit="true" runat="server">
                                </ext:GridView>
                            </View>
                            <SelectionModel>
                                <ext:CheckboxSelectionModel ID="checkSelectionModel" runat="server" IDMode="Static">
                                </ext:CheckboxSelectionModel>
                            </SelectionModel>
                        </ext:GridPanel>
                    </td>
                </tr>
                <tr>
                    <td>
                            <ext:TextField runat="server" ID="txtbankAccountAndDues" meta:resourcekey="txtbankAccountAndDuesResource"
                            Disabled="true">
                        </ext:TextField>

                    </td>
                </tr>
            </table>
            </Body>
            <BottomBar>
                <ext:StatusBar runat="server" ID="statusBar" IDMode="Static" meta:resourcekey="statusBarResource1">
                    <Items>
                        <ext:Button runat="server" ID="btnPrint" Icon="PrinterGo">
                            <AjaxEvents>
                                <Click OnEvent="BtnPrint_Clicked">
                                    <ExtraParams>
                                        <ext:Parameter Name="WaitingLoansGrid" Value="Ext.encode(#{gridWaitingLoans}.getRowsValues(false))"
                                            Mode="Raw" />
                                    </ExtraParams>
                                </Click>
                            </AjaxEvents>
                        </ext:Button>
                        <ext:Button runat="server" ID="btnBack" Text="Back" Icon="ArrowUndo" meta:resourcekey="btnBackResource1">
                            <AjaxEvents>
                                <Click OnEvent="btnBack_Clicked" />
                            </AjaxEvents>
                        </ext:Button>
                        <ext:Button ID="btnSave" runat="server" Text="<%$ Resources:btnSaveText %>" Icon="TableSave">
                            <AjaxEvents>
                                <Click OnEvent="BtnSave_Clicked" Before="return ValidateForm();">
                                    <EventMask ShowMask="true" />
                                    <ExtraParams>
                                        <ext:Parameter Name="WaitingLoansGrid" Value="Ext.encode(#{gridWaitingLoans}.getRowsValues(false))"
                                            Mode="Raw" />
                                    </ExtraParams>
                                </Click>
                            </AjaxEvents>
                        </ext:Button>
                        <quadz:CoolitePermitButton runat="server" ID="btnSubmit" Text="Submit" Icon="Accept"
                            meta:resourcekey="btnSubmitResource1" PermissionName="SubmitCreateBoard" HideAsDisabled="true">
                            <AjaxEvents>
                                <Click OnEvent="btnSubmit_Clicked" Before="return ValidateForm();">
                                    <EventMask MinDelay="100" Msg="<%$ Resources:TextCreatingBoardLoan %>" Target="CustomTarget"
                                        CustomTarget="#{mainForm}" ShowMask="true" />
                                    <ExtraParams>
                                        <ext:Parameter Name="WaitingLoansGrid" Value="Ext.encode(#{gridWaitingLoans}.getRowsValues(false))"
                                            Mode="Raw" />
                                    </ExtraParams>
                                </Click>
                            </AjaxEvents>
                        </quadz:CoolitePermitButton>
                    </Items>
                </ext:StatusBar>
            </BottomBar>
        </ext:FormPanel>
    </ext:FitLayout>

    <ext:Window runat="server" ID="wndBoardPrint" Modal="true" ShowOnLoad="false" Width="550px"
        Height="480px">
        <AutoLoad Mode="IFrame" ShowMask="True" ManuallyTriggered="True" PassParentSize="True" />
        <AjaxEvents>
            <Remove OnEvent="wndBoardPrintPrint_Closed" />
            <Close OnEvent="wndBoardPrintPrint_Closed" />
        </AjaxEvents>
    </ext:Window>

    <ext:Window runat="server" ID="wndSelectMember" Modal="true" ShowOnLoad="false" Width="550px" Height="450px">
        <Body>
            <ext:FitLayout runat="server">
            <ext:GridPanel runat="server" ID="gridSearchResults" IDMode="Static" AutoHeight="true" Header="false"
                                    AutoExpandColumn="Name" StoreID="storeResults" meta:resourcekey="gridSearchResultsResource1">
                         <ColumnModel>
                                        <Columns>
                                            <ext:Column ColumnID="Name" Header="<%$ Resources:HeaderName %>" DataIndex="Name"
                                                Sortable="True" />
                                            <ext:CommandColumn Header="<%$ Resources:HeaderAddToBoard %>" Hideable="False" Sortable="True">
                                                <Commands>
                                                    <ext:GridCommand CommandName="AddMember" Text="<%$ Resources:TextAdd %>" Icon="Add"
                                                        IconCls="icon-add" />
                                                </Commands>
                                            </ext:CommandColumn>
                                        </Columns>
                                    </ColumnModel>
                         <View>
                                        <ext:GridView ID="GridView1" runat="server">
                                        </ext:GridView>
                                    </View>
                         <AjaxEvents>
                                        <Command OnEvent="gridMembers_Command">
                                            <ExtraParams>
                                                <ext:Parameter Name="CommandName" Value="command" Mode="Raw" Encode="False" />
                                                <ext:Parameter Name="UserId" Value="record.data.Id" Mode="Raw" Encode="False" />
                                                <ext:Parameter Name="DisplayName" Value="record.data.Name" Mode="Raw" Encode="False" />
                                            </ExtraParams>
                                            <EventMask MinDelay="200" Msg="<%$ Resources:TextUpdating %>" Target="CustomTarget"
                                                CustomTarget="#{gridBoardMembers}" ShowMask="True" />
                                        </Command>
                                    </AjaxEvents>
            </ext:GridPanel>     
            </ext:FitLayout>
        </Body>
    </ext:Window>
</asp:Content>
